package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.AssetRecordAdapter;
import com.wnhz.work.jy.bean.AssetItemBean;
import com.wnhz.work.jy.bean.AssetRecordBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TakeGlodRecordActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "TakeGlodRecordActivity";
    private ListView listView;
    private TextView clock1, clock2;
    private int type;
    private TextView title;
    private AssetRecordAdapter adapter;
    private List<AssetRecordBean> sellDataArrlist = new ArrayList<>();
    private List<AssetRecordBean> sellFinishDataArrlist = new ArrayList<>();
    private List<AssetRecordBean> tackDataArrlist = new ArrayList<>();
    private List<AssetRecordBean> tackFinishDataArrlist = new ArrayList<>();
    private List<AssetRecordBean> Arrlist = new ArrayList<>();
    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;

    private View line1, line2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_glod_record);
        x.view().inject(this);
        listView = (ListView) findViewById(R.id.listView);
        clock1 = (TextView) findViewById(R.id.clock1);
        clock2 = (TextView) findViewById(R.id.clock2);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        clock1.setOnClickListener(this);
        clock2.setOnClickListener(this);
        title = (TextView) findViewById(R.id.view_main_middle_title);

        findViewById(R.id.btn_main_left).setOnClickListener(this);
        adapter = new AssetRecordAdapter(this, Arrlist);

        adapter.setListener(listener);
        type = getIntent().getIntExtra("type", -1);
        setTextColor(true);
        String url = null;
        if (type == 3) {
            title.setText("提金记录");
            url = ConfigUtil.TAKERECORD;
        } else if (type == 2) {
            title.setText("卖金记录");
            url = ConfigUtil.SELLRECORD;
        }
        listView.setAdapter(adapter);
        uploadByxUtils(url);
        setBlank();
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent;
            int index;
            switch (v.getId()) {
                case R.id.onclick:
                case R.id.lay:
                    index = (int) v.getTag();
                    if (type == 3) {
                        intent = new Intent(TakeGlodRecordActivity.this, TakeGoldRecordDetailsActivity.class);
                        intent.putExtra("orderId", Arrlist.get(index).getOrderId());
                        startActivity(intent);
                    }
                    break;

                default:
                    break;
            }
        }
    };

    private void setBlank() {
        if (Arrlist.size() == 0) {
            blank_tv.setText("还没有该状态下的记录哦~");
            blank_img.setImageResource(R.mipmap.ic_kongdingdan);
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    private void setTextColor(boolean b) {
        if (b) {
            clock1.setTextColor(0xffe2bb0a);
            clock2.setTextColor(0xff666666);
        } else {
            clock2.setTextColor(0xffe2bb0a);
            clock1.setTextColor(0xff666666);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clock1:
                setTextColor(true);
                if (type == 3) {
                    Arrlist.clear();
                    Arrlist.addAll(tackDataArrlist);

                } else if (type == 2) {
                    Arrlist.clear();
                    Arrlist.addAll(sellDataArrlist);
                }
                setBlank();
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                adapter.notifyDataSetChanged();
                break;
            case R.id.clock2:
                setTextColor(false);
                if (type == 3) {
                    Arrlist.clear();
                    Arrlist.addAll(tackFinishDataArrlist);

                } else if (type == 2) {
                    Arrlist.clear();
                    Arrlist.addAll(sellFinishDataArrlist);
                }
                setBlank();
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                adapter.notifyDataSetChanged();
                break;
            case R.id.btn_main_left:
                finish();
                break;
        }
    }

    private void uploadByxUtils(String url) {
        Map<String, Object> map = new HashMap<>();
        showDialog();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                Arrlist.clear();
                try {
                    JSONObject json = new JSONObject(result);
                    if (type == 3) {
                        JSONArray tackDataArr = json.optJSONArray("tackDataArr");
                        tackDataArrlist.clear();
                        if (tackDataArr != null) {
                            for (int i = 0; i < tackDataArr.length(); i++) {
                                AssetRecordBean assetRecordBean = new AssetRecordBean();
                                assetRecordBean = gson.fromJson(tackDataArr.optString(i), AssetRecordBean.class);
                                assetRecordBean.setMyType(type);
                                JSONArray array4 = tackDataArr.optJSONObject(i).optJSONArray("goodsInfo");
                                ArrayList<AssetItemBean> assetItemBeans = new ArrayList<>();
                                for (int i1 = 0; i1 < array4.length(); i1++) {
                                    AssetItemBean assetItemBean = new AssetItemBean();
                                    assetItemBean = gson.fromJson(array4.optString(i1), AssetItemBean.class);
                                    assetItemBeans.add(assetItemBean);
                                }
                                assetRecordBean.setOrderDetail(assetItemBeans);
                                tackDataArrlist.add(assetRecordBean);
                            }

                        }
                        JSONArray tackFinishDataArr = json.optJSONArray("tackFinishDataArr");
                        tackFinishDataArrlist.clear();
                        if (tackFinishDataArr != null) {
                            for (int i = 0; i < tackFinishDataArr.length(); i++) {
                                AssetRecordBean assetRecordBean = new AssetRecordBean();
                                assetRecordBean = gson.fromJson(tackFinishDataArr.optString(i), AssetRecordBean.class);
                                assetRecordBean.setMyType(type);
                                JSONArray array4 = tackFinishDataArr.optJSONObject(i).optJSONArray("goodsInfo");
                                ArrayList<AssetItemBean> assetItemBeans = new ArrayList<>();
                                for (int i1 = 0; i1 < array4.length(); i1++) {
                                    AssetItemBean assetItemBean = new AssetItemBean();
                                    assetItemBean = gson.fromJson(array4.optString(i1), AssetItemBean.class);
                                    assetItemBeans.add(assetItemBean);
                                }
                                assetRecordBean.setOrderDetail(assetItemBeans);
                                tackFinishDataArrlist.add(assetRecordBean);
                            }

                        }

                        Arrlist.addAll(tackDataArrlist);

                    } else if (type == 2) {

                        JSONArray array1 = json.optJSONArray("sellDataArr");
                        if (array1 != null) {
                            for (int i = 0; i < array1.length(); i++) {
                                AssetRecordBean recordBean = new AssetRecordBean();
                                List<AssetItemBean> orderDetail = new ArrayList<>();
                                AssetItemBean assetItemBean = new AssetItemBean();

                                recordBean = gson.fromJson(array1.optString(i), AssetRecordBean.class);
                                recordBean.setMyType(type);
                                assetItemBean = gson.fromJson(array1.optString(i), AssetItemBean.class);
                                orderDetail.add(assetItemBean);
                                recordBean.setOrderDetail(orderDetail);
                                sellDataArrlist.add(recordBean);
                            }
                            Arrlist.addAll(sellDataArrlist);
                        }
                        JSONArray array2 = json.optJSONArray("sellFinishDataArr");
                        if (array2 != null) {
                            for (int i = 0; i < array2.length(); i++) {
                                AssetRecordBean recordBean = new AssetRecordBean();
                                List<AssetItemBean> orderDetail = new ArrayList<>();
                                AssetItemBean assetItemBean = new AssetItemBean();
                                recordBean = gson.fromJson(array2.optString(i), AssetRecordBean.class);
                                recordBean.setMyType(type);
                                assetItemBean = gson.fromJson(array2.optString(i), AssetItemBean.class);
                                orderDetail.add(assetItemBean);
                                recordBean.setOrderDetail(orderDetail);
                                sellFinishDataArrlist.add(recordBean);
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                setBlank();
                closeDialog();
            }
        });
    }
}
